package com.example.demo7;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:ChenSheng
 * @Description:
 * @Date: Created in 下午 14:06 2018/6/14 0014
 */
@Controller
public class IndexController {

	@RequestMapping("/say")
	@ResponseBody
	public User say(){
		User user = new User();
		user.setId(12);
		user.setName("wangyue");
		return user;
	}

	@PostMapping("/add")
	@ResponseBody
	public String add(){
		System.out.println("I am userlist");

		return "succ";
	}

	@PostMapping("/addListGoodFormat")
	@ResponseBody
	public String addList(@RequestBody UserListGoodFormat userListGoodFormat){
		//验证成功，出现的JsonMappingException: out of START_ARRAY token这个错误是类型不匹配造成的，
		// 比如[{},{}]对应的是List<Map>类型，而我写了String，会报错，但其实我的本意是把这个字符串保存成json那样的，
		//JsonMappingException: out of START_ARRAY token exception is thrown by Jackson object mapper as it's expecting an Object {} whereas it found an Array [{}] in response.
		System.out.println(userListGoodFormat.getId());
		System.out.println(userListGoodFormat.getRows());
		System.out.println("I am userListGoodFormat");
		return "succ List";
	}

	@PostMapping("/addListBadFormat")
	@ResponseBody
	public String addList(@RequestBody UserListBadFormat userListBadFormat){
		//验证成功，出现的JsonMappingException: out of START_ARRAY token这个错误是类型不匹配造成的，
		// 比如[{},{}]对应的是List<Map>类型，而我写了String，会报错，但其实我的本意是把这个字符串保存成json那样的，
		//JsonMappingException: out of START_ARRAY token exception is thrown by Jackson object mapper as it's expecting an Object {} whereas it found an Array [{}] in response.
		System.out.println(userListBadFormat.getId());
		System.out.println(userListBadFormat.getRows());
		System.out.println("I am userListBadFormat");
		return "succ List";
	}

	//经测试发现testMap 和 testUser返回的是一样的 Content-Type: application/json;charset=UTF-8  {"name":"陈胜","id":12} 但就是里面的属性前后顺序有不同而已
	@RequestMapping("/testUser")
	@ResponseBody
	public User testUser(){
		User user = new User();
		user.setId(12);
		user.setName("陈胜");
		return user;
	}

	@RequestMapping("/testMap")
	@ResponseBody
	public Map testMap(){
		Map<String, Object> map = new HashMap<>();
		map.put("id", 12);
		map.put("name","陈胜");
		return map;
	}


}
